package logic

import (
	"context"

	"gitee.com/gao-deguang/easy-chat/apps/social/rpc/internal/svc"
	"gitee.com/gao-deguang/easy-chat/apps/social/rpc/social"
	"gitee.com/gao-deguang/easy-chat/pkg/xerr"

	"github.com/jinzhu/copier"
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type GroupListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGroupListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GroupListLogic {
	return &GroupListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GroupListLogic) GroupList(in *social.GroupListReq) (*social.GroupListResp, error) {
	// todo: add your logic here and delete this line
	groupids, err := l.svcCtx.GroupMembersModel.FindByUserId(l.ctx, in.UserId)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewDBErr(), "GroupList->FindByUserIdErr:%v req:%v", err, in)
	}

	ids := []string{}
	for _, id := range groupids {
		ids = append(ids, id.GroupId)
	}
	resp, err := l.svcCtx.GroupsModel.FindByIds(l.ctx, ids...)
	if err != nil {
		return nil, err
	}
	var list []*social.Groups
	copier.Copy(&list, &resp)
	return &social.GroupListResp{List: list}, nil
}
